#下载一部电影

import requests
import re,os
from lxml import etree
from urllib.request import  urlretrieve    #使程序智能化，自动创建一个临时的文件进行保存
for page in range(1,5):
    if page ==  1:
        url = 'http://www.pearvideo.com/category_1'
    else:
        url = 'http://www.pearvideo.com/category_%d'%page
        #把文本文件处理成而解析对象
        html = requests.get(url).text

        html = etree.HTML(html)
        # print(html)

        video_id = html.xpath('//div[@class="vervideo-bd"]/a/@href')
        # print(video_id)

        video_url = []
        start_url = 'http://www.pearvideo.com/'

        #创建保存视频的文件夹
        if 'video' not in os.listdir():
            os.makedirs('video')

        for id in video_id:
            video_url.append(start_url + id)
        # print(video_url)
        #video标签不是自己写的html,而是js加载的
        #所以需要通过正则表达式来匹配出视频的播放地址

        for play_url in video_url:
            # print(play_url)

            html2 = requests.get(play_url).text
            # print(html2)
            #正则匹配
            req_url = 'srcUrl="(.*?)"'
            #视频真正的播放地址
            purl= re.findall(req_url,html2)
            print(purl)

            #获取视频名称

            req_video_url = '<h1 class="video-tt">(.*?)</h1>'
            video_name = re.findall(req_video_url,html2)
            # print(video_name[0])
            video_name = video_name[0].replace(r'"','')
            #下载url,下载的地址
            print(video_name)
            urlretrieve(purl[0],'video/%s.mp4'%video_name)